package com.lz;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQConnector {
    private static Connection connection;
    private static Channel channel;

    /**
     * 创建一个新的 RabbitMQ 连接和通道。
     *
     * @return 通道对象
     */
    public static Channel createConnectionAndChannel() throws Exception {
        // 创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        // 创建连接
         connection = factory.newConnection();
        // 创建通道
         channel = connection.createChannel();
        return channel;
    }
    /**
     * 关闭通道和连接。
     */
    public static void closeResources() throws Exception {
        if (channel != null && channel.isOpen()) {
            channel.close();
        }
        if (connection != null && connection.isOpen()) {
            connection.close();
        }
    }
}